/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "AlarmBalloon.hpp"
#include "Devices.hpp"
#include "math.h"
#include <GL/gl.h>
#include <GL/glut.h>


#define BUBBLE_W (10.0/view.Scale)

using namespace MuRo;

CAlarmBalloon::CAlarmBalloon(CAlarmDevice *device, MaCI::Alarm::CAlarmClient *client, 
                             const MaCI::Alarm::CAlarmData data)
  :client(client),data(data),device(device),pickingContext(NULL)
{
  SetCallback(this);
  
  if (data.GetAlarmEvent()->type==MaCI::Alarm::KEventTypeQuestion)
  {
    messageBox=new MuRo_QuestionFrame(NULL, this);
  } else {
    messageBox=new MuRo_AlarmFrame(NULL, this);
  }
}

CAlarmBalloon::~CAlarmBalloon()
{
  if (pickingContext)
    pickingContext->RemovePickable(this);
  
  delete messageBox;
}

void CAlarmBalloon::Draw(const TViewParams &view)
{
  this->view=view;
  
  char c[2]={'!',0};
  
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  
  if (data.GetAlarmEvent()->type==MaCI::Alarm::KEventTypeQuestion)
  {
    glColor3f(1,1,1);
    c[0]='?';
  } else if (data.GetAlarmEvent()->type==MaCI::Alarm::KEventTypeWarning || 
            data.GetAlarmEvent()->type==MaCI::Alarm::KEventTypeError) 
  {
    glColor3f(1,0.6,0.6);
    c[0]='!';
  } else {
    glColor3f(1,1,1);
    c[0]='!';
  }
  
  glBegin(GL_QUADS);

  glVertex3f(posX-BUBBLE_W, posY+ -BUBBLE_W,15);
  glVertex3f(posX+BUBBLE_W, posY+ -BUBBLE_W,15);
  glVertex3f(posX+BUBBLE_W, posY+ BUBBLE_W,15);
  glVertex3f(posX-BUBBLE_W, posY+ BUBBLE_W,15);
      
  glEnd();
  glColor3f(0,0,0);

  /**
  printf("Drawin bubble: X:%f Y:%f Z:%f. Text: X:%f Y:%f Z:%f char:%c\n", posX-BUBBLE_W, posY+ -BUBBLE_W,15.0,posX-3.0/view.Scale,posY-8.0/view.Scale,15.0,c[0]);
  */

  drawText(posX-3.0/view.Scale,posY-8.0/view.Scale,15.1,GLUT_BITMAP_HELVETICA_18,c);
  
}

void CAlarmBalloon::DrawPickable()
{
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  
  glBegin(GL_QUADS);

  glVertex3f(posX-BUBBLE_W, posY+ -BUBBLE_W,15);
  glVertex3f(posX+BUBBLE_W, posY+ -BUBBLE_W,15);
  glVertex3f(posX+BUBBLE_W, posY+ BUBBLE_W,15);
  glVertex3f(posX-BUBBLE_W, posY+ BUBBLE_W,15);
  
  glEnd();
}

void CAlarmBalloon::SetPickingContext(CGLPickingContext *context)
{
  if (pickingContext)
    pickingContext->RemovePickable(this);
  pickingContext=context;
  pickingContext->AddPickable(this);
}

void CAlarmBalloon::OnPick(TGLPickEvent event)
{
  if (!(messageBox->IsShown()))
  {
    //show message box
    Expand(true);
  } else {
    //bring message box to front
    messageBox->Raise();
  }
}

void CAlarmBalloon::Expand(bool expand)
{
  messageBox->Show(expand);
}

void CAlarmBalloon::SetPosition(float x, float y)
{
  posX=x;
  posY=y;
}

void CAlarmBalloon::SetSourcePosition(float x, float y)
{
  sourceX=x;
  sourceY=y;
}

void CAlarmBalloon::Destroy()
{
  device->RemoveAlarm(this);
}


